namespace MetaProperties.SynchronizationContexts
{
    using System;
    using System.Threading;

    [Serializable]
    internal class WorkItem
    {
        private readonly SendOrPostCallback method;

        private readonly object state;

        private readonly ManualResetEvent waitHandle;

        internal WorkItem(SendOrPostCallback method, object state)
        {
            this.method = method;
            this.state = state;
            this.waitHandle = new ManualResetEvent(false);
        }

        public WaitHandle WaitHandle
        {
            get { return this.waitHandle; }
        }

        /// <summary>
        /// This method is called on the worker thread to execute the method.
        /// </summary>
        internal void Execute()
        {
            this.method(this.state);
            this.waitHandle.Set();
        }
    }
}